package com.developmentReformProject.controller;

import com.alibaba.fastjson.JSON;
import com.developmentReformProject.dao.FiveProjectDao;
import com.developmentReformProject.dto.FiveQueryDto;
import com.developmentReformProject.dto.ThreeQueryDto;
import com.developmentReformProject.entity.FiveYearProject;
import com.developmentReformProject.service.FiveYearProjectLibraryService;
import com.developmentReformProject.utils.FileUtil;
import com.developmentReformProject.utils.PageUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/five")
public class FiveController {
    @Autowired
    private FiveProjectDao fiveProjectDao;
    @Autowired
    private FiveYearProjectLibraryService fiveYearProjectLibraryService;

    @GetMapping("/queryList")
    public ResultVo list(FiveQueryDto fiveQueryDto) throws Exception {
        //调用业务
        PageUtil<FiveYearProject> data = fiveYearProjectLibraryService.pageList(fiveQueryDto);
        //响应
        ResultVo resultVo = new ResultVo<>(data);
        return resultVo;
    }

    @PostMapping("/delete")
    public ResultVo deleteEnterpriseAdmin(int fiveProjectNo) throws Exception {
        fiveProjectDao.deleteProject(fiveProjectNo);
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    @PostMapping("/update")
    public ResultVo updateEnterpriseAdmin(FiveYearProject fiveYearProject) throws Exception {
        fiveProjectDao.updateFive(fiveYearProject);
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    @PostMapping("/addProject")
    public ResultVo addProject(FiveYearProject fiveYearProject) throws Exception {
        fiveProjectDao.addFive(fiveYearProject);
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    @GetMapping("/download")
    public void download(int fiveProjectNo, HttpServletResponse resp) throws Exception {
        FiveYearProject fiveYearProject = fiveProjectDao.queryByNo(fiveProjectNo);
        String filename = fiveYearProject.getFiveProjectAssignmentPaper();
        FileUtil.download(filename, resp);//下载数据
    }

    @GetMapping("/listDelete")
    public ResultVo listDelete(FiveQueryDto fiveQueryDto) throws Exception {
        PageUtil<FiveYearProject> data = fiveYearProjectLibraryService.cleanFive(fiveQueryDto);
        ResultVo resultVo = new ResultVo<>(data);
        return resultVo;
    }

    @PostMapping("/reduction")
    public ResultVo reduction(int fiveProjectNo) throws Exception {
        FiveYearProject fiveYearProject = new FiveQueryDto();
        fiveYearProject.setFiveProjectNo(fiveProjectNo);
        fiveYearProject.setFiveStateId(1);
        fiveProjectDao.updateFive(fiveYearProject);
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    @PostMapping("/cleanFive")
    public ResultVo cleanFive(int fiveProjectNo) throws Exception {
        fiveProjectDao.cleanFive(fiveProjectNo);
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    @GetMapping("/fiveCount")
    public int threeCount() throws Exception {
        FiveQueryDto fiveQueryDto = new FiveQueryDto();
        int mag = fiveProjectDao.countSelective(fiveQueryDto);
        return mag;
    }
    @GetMapping("/count")
    public String count() {
        List<Integer> list=new ArrayList();
        list.add(fiveProjectDao.countOne());
        list.add(fiveProjectDao.countTwo());
        list.add(fiveProjectDao.countThree());
        list.add(fiveProjectDao.countFour());
        list.add(fiveProjectDao.countFive());
        list.add(fiveProjectDao.countSix());
        System.out.println(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }
}
